<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.mongodb.client.MongoCursor, org.bson.Document, org.bson.types.ObjectId, mongo.Class, mongo.Major" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../lib/sweetalert/sweetalert.css">
<script src="../../lib/sweetalert/sweetalert.min.js"></script>
<title>更新班级信息</title>
<script>
	//表单提交前进行数据验证，数据合法则返回true表单提交
	function check(classform) {  
		var myselect = document.getElementById("selectMajor");
	    var index = myselect.selectedIndex ;             // selectedIndex代表的是你所选中项的index
	    classform.major.value =  myselect.options[index].text; //拿到选中项options的text
	   	if(classform.name.value=="") {  
	    	swal("专业名称不能为空","请重新输入","warning");   
	        return false;  
	    }  
	    else if(classform.majorid.value=="") {
	    	swal("所属系部不能为空","请重新输入","warning");   
	        return false;  
	    } 

	    return true;  
	}   
</script>
</head>
<body>
	<%
	    Class Class = new Class();
	    ObjectId _id = new ObjectId(request.getParameter("_id"));
		Document document = Class.findByid(_id);
		String id = document.getString("id");
		String name = document.getString("name");
		String major = document.getString("major");
		ObjectId majorid = document.getObjectId("majorid");
		String remark = document.getString("remark");
	%>
	<jsp:include page="../navbar.html" />
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-1 col-md-offset-3">
				<a href="classServlet?method=list">
					<button type="button" class="btn btn-default"><em class="fa fa-chevron-left"></em> 返回</button>
				</a>
			</div>
		</div>
    	<div class="row mt20">
			<div class="col-md-6 col-md-offset-3">
				<div class="panel">
                	<div class="panel-body">
                  		<h4 class="page-header mt0">修改班级信息</h4>
                  			<form name="classform" role="form" action="classServlet" method="post"  onsubmit="return check(this);" class="form-horizontal mt15">
	                  	    <fieldset>
				              <div class="form-group">
				                  <label class="col-md-2 control-label">ID</label>
				                  <div class="col-md-9">
				                     <input class="form-control" type="text" name="id" value="<%=id %>" placeholder="请输入班级ID" readonly />
				                  </div>
				               </div> 
			                </fieldset>
		               		<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">名称</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="text" name="name" value="<%=name %>" placeholder="请输入班级名称" />
		                  			</div>
		               			</div>	
		               		</fieldset>  
		               		<fieldset>
		               			 <div class="form-group">
					               <label class="col-sm-2 control-label">所属专业</label>
					               <div class="col-sm-9">
					                  <select class="form-control" name="majorid" id="selectMajor">
						                <%
						                	Major Major = new Major();
					                		MongoCursor<Document> majorList = Major.list(); 
					                  		while(majorList.hasNext()) {
					                  			Document result = majorList.next();%>
					                     	<option value="<%=result.getObjectId("_id") %>"  <%=majorid.equals(result.getObjectId("_id")) ? "selected" : ""%>><%=result.getString("name") %></option>
					                    <%} %> 
					                  </select>
					              </div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group">
			               			<label class="col-md-2 control-label">备注</label>
				               		<div class="col-md-9">
			               				<textarea class="form-control no-resize" name="remark" placeholder="请输入班级备注" rows="6"><%=remark %></textarea>
			               			</div>
			               		</div>
		               		</fieldset>  
		               		<input type="hidden" name="major" id="major">
		               		<input type="hidden" name="_id" value="<%=_id %>">
		               		<div class="col-md-10 col-md-offset-1">
		               			<div class="col-md-6">
		               				<button type="submit" class="btn btn-primary btn-block" name="method" value="update">确定</button> 
		               			</div>
		               			<div class="col-md-6">
		               				<button type="reset" class="btn btn-default btn-block">重置</button> 
		               			</div>
		               		</div>
		               		
	              			</form>
               		</div>
            	</div>
         	</div>
       </div>
    </div>
</body>
</html>